<?php
class LanguageFilter extends CFilter
{
		private $_oldLanguage;
		public function filter($filterChain){
			if($this->preFilter($filterChain))
			{
					$filterChain->run();
					$this->postFilter($filterChain);
			}
		}
		
		// triggered before action
    protected function preFilter($filterChain){
			$lang = cmd::cookie('language');
			if (isset($_REQUEST['lang']) && in_array($_REQUEST['lang'],array_keys(Yii::app()->params['languages']))) {
				Yii::app()->language = $_REQUEST['lang'];
				//write in cookie
				cmd::cookie(array('language',$_REQUEST['lang']));
			} elseif (!empty($lang)){
				Yii::app()->language = $lang;
			}
			Yii::app()->user->setFlash('languageChangeTo',Yii::app()->language);
			return true; // return false to ignore action
    }

		// triggered after action
    protected function postFilter($filterChain)
    {
			$this->_oldLanguage=Yii::app()->language;
			Yii::app()->user->setFlash('languageChangeFrom',$this->_oldLanguage);
    }
}